const dbUtils = require('./../utils/db-util')
const question = {
  /**
 * @param {上下文对象} ctx 
 * @param {加载问答列表  所有}  空参
 * @param {精选问题}   jxAsk
 * @param {付费问题}   payAsk
 * @param {精选问题}   jxAsk
 * @param {最新问题}   newAsk
 * @param {待回答问题}     waitAsk
 * 
 */
  async getQst(page,sort) {
    let offset = (page - 1) * 12;
    if (sort === 'jxAsk') {
      var _sql = `SELECT question.*,answer.user,answer.time,answer.answerCon from question ,answer  where question.id = answer.askid and question.good > 0  group by question.title limit ${offset}, 10`
    } else if (sort === 'payAsk') {
      var _sql = `SELECT question.*,answer.user,answer.time,answer.answerCon from question,answer where question.id = answer.askid and question.pay > 0 group by question.title limit ${offset}, 10`
    } else if (sort === 'newAsk') {
      var _sql = `SELECT * FROM question order by stu_time desc`
    } else if (sort === 'waitAsk') {
      var _sql = `SELECT * from question,answer where question.id <> answer.askid limit ${offset}, 10`
    } else {
      var _sql = `SELECT * from question limit ${offset}, 10`
    }
    console.log(_sql)
    let _sqlCount = `SELECT COUNT (id) from question `
    let resData = {}
    let result = await dbUtils.query( _sql )
    let count = await dbUtils.query( _sqlCount )
    if ( Array.isArray(result) && result.length > 0 ) {
      resData.result = result
      let num = JSON.stringify(count).replace(/[^0-9]/ig,"");
      resData.count = num
    } else {
      resData = null
    }
    return resData
  },

  async getQstlike(findCon) {
    let _sqlCount = `SELECT * FROM question WHERE title LIKE '%${findCon}%' or stu_con LIKE '%${findCon}%' `
    let resData = {}
    let result = await dbUtils.query( _sqlCount )
    if ( Array.isArray(result) && result.length > 0 ) {
      resData.result = result
    } else {
      resData = null
    }
    return resData
  },

  /**
 * @param {获取详细信息} findCon 
 */
  async getDetils(id) {
    let _sql = `SELECT * FROM question,answer WHERE question.id = ${id} and answer.askid = ${id} `
    let resData = {}
    let result = await dbUtils.query( _sql)
    if (Array.isArray(result) && result.length > 0 ) {
      resData.result = result
    } else {
      resData = null
    }
    return resData
  },

  /**
   * 
   * @param {回答问题} form 
   */
  async userAddask(form) {
    let result = await dbUtils.insertData( 'answer', form )
    return result
  },

  async userQuestion(form) {
    let result = await dbUtils.insertData( 'question', form )
    return result
  },
  /**
   * 
   * @param {获取用户回答问题} form 
   */
  async userQstTitle(name) {
    let _sql = `SELECT time,stu_name,title FROM answer,question WHERE answer.user = '${name}' and answer.askid = question.id `
    let resData = {}
    let result = await dbUtils.query( _sql)
    if (Array.isArray(result) && result.length > 0 ) {
      resData.result = result
    } else {
      resData = null
    }
    return resData
  },

    /**
   * 
   * @param {获取用户发表信息} name 
   */
  async getUserAddQst(name) {
    let _sql = `SELECT * FROM question WHERE question.stu_name = '${name}'`
    let resData = {}
    let result = await dbUtils.query( _sql)
    if (Array.isArray(result) && result.length > 0 ) {
      resData.result = result
    } else {
      resData = null
    }
    return resData
  },
}
module.exports = question